﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text;

namespace UploadForm.Common
{
    public class Utils
    {

        public static string GetDeviceRootDir(bool needShowTip = false)
        {
            DriveInfo[] s = DriveInfo.GetDrives();
            var result = string.Empty;
            foreach (DriveInfo i in s)
            {
                if (i.DriveType == DriveType.Removable)
                {
                    if (i.VolumeLabel.Contains("BMECG"))
                    //if (i.VolumeLabel.Contains("TOSHIBA"))
                    //if (i.VolumeLabel.Contains("U大师启动盘"))
                    {
                        result = i.RootDirectory.ToString();
                        break;
                    }
                }
            }

            var drivers = s.Where(item => item.DriveType == DriveType.Removable && item.VolumeLabel.Contains("BMECG")).ToList() ;
            if (drivers.Count() > 1)
            {
                if (needShowTip)
                {
                    System.Windows.Forms.MessageBox.Show("亲~ 该程序只能同时回收一台设备的数据哦。");
                }
                return string.Empty;
            }
            return result;
        }



        public static string GetMD5HashFromBytes(byte[] bytes)
        {
            try
            {
                //FileStream file = new FileStream(fileName, FileMode.Open);
                //System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                //byte[] retVal = md5.ComputeHash(file);
                //file.Close();
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(bytes);

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
            }
        }


        public static List<T> GetList<T>(SQLiteDataReader reader)
        {
            List<T> result = new List<T>();
            if (reader == null)
                return new List<T>();

            while (reader.Read())
            {
                T item = System.Activator.CreateInstance<T>();
                var type = typeof(T);
                var properties = type.GetProperties();

                var length = reader.FieldCount;
                for (int i = 0; i < length; i++)
                {
                    var name = reader.GetName(i);
                    var property = properties.Where(s => s.Name == name).FirstOrDefault();
                    if (property != null)
                    {

                        if (property.PropertyType.Name == "String")
                        {
                            var value = reader.GetString(reader.GetOrdinal(property.Name));
                            if (!string.IsNullOrEmpty(value))
                            {
                                property.SetValue(item, value, null);
                            }
                        }
                        else if (property.PropertyType.Name == "Int32")
                        {
                            var value = reader.GetInt32(reader.GetOrdinal(property.Name));
                            if (value != 0)
                            {
                                property.SetValue(item, value, null);
                            }
                        }
                        else if (property.PropertyType.Name == "Int64")
                        {
                            var value = reader.GetInt64(reader.GetOrdinal(property.Name));
                            if (value != 0)
                            {
                                property.SetValue(item, value, null);
                            }
                        }
                    }
                }
                result.Add(item);
            }
            return result;
        }

    }
}
